package admin

import (
	"fmt"
	"strconv"
	"strings"
	"tcsc/models"
	"tcsc/sword"

	"github.com/astaxie/beego"
)

type System struct {
	BaseController
}

func (c *System) Get() {
	if c.Gid == "0" {
		info := models.Systems()
		province := models.Query(`select * from mt_area where pid=0`)
		c.Data["province"] = province
		if shen, ok := info["province"].(string); ok {
			city := models.Query(`select a.* from mt_area a left join mt_area b on a.pid=b.id where b.code=` + shen)
			c.Data["city"] = city
			if shi, ok := info["city"].(string); ok {
				c.Data["area"] = models.Query(`select a.* from mt_area a left join mt_area b on a.pid=b.id where b.code=` + shi)
			} else {
				c.Data["area"] = models.Query(`select a.* from mt_area a left join mt_area b on a.pid=b.id where b.code=` + city[0]["id"].(string))
			}
		} else {
			city := models.Query(`select a.* from mt_area a left join mt_area b on a.pid=b.id where b.code=` + province[0]["id"].(string))
			c.Data["city"] = city
			c.Data["area"] = models.Query(`select a.* from mt_area a left join mt_area b on a.pid=b.id where b.code=` + city[0]["id"].(string))
		}
		c.Data["info"] = info
		c.TplName = "admin/systems/index.html"
	} else {
		c.Data["cid"] = models.Query(`select id,name from mt_column where type='p' and status=1 and pid=0`)
		imgs := make(map[int]string)
		picnum := make(map[int]string)
		for i := 0; i < 4; i++ {
			imgs[i] = "/static/img/admin/uppic.png"
			picnum[i] = ""
		}
		if info := models.Query(`select a.*,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id where a.id=` + c.Gid); info != nil {
			picture := fmt.Sprint(info[0]["companyimg"])
			if picture != "" && picture != "<nil>" {
				if imgss := models.Query(fmt.Sprintf(`select id,pathname from mt_resources where id in(%v)`, picture)); imgss != nil {
					for k, v := range imgss {
						imgs[k] = fmt.Sprint(v["pathname"])
						picnum[k] = fmt.Sprint(v["id"])
					}
				}
			}
			c.Data["info"] = info[0]
		}
		c.Data["imgs"] = imgs
		c.Data["picnum"] = picnum
		c.TplName = "admin/systems/index_g.html"
	}
}
func (c *System) Post() {
	defer c.ServeJSON()
	input := c.Input()
	var res int64
	var err error
	if c.Gid == "0" {
		res, err = models.Save("mt_systems", input)
	} else {
		input.Set("companyimg", strings.Trim(strings.Join(c.GetStrings("companyimg"), ","), ","))
		input.Set("type", strings.Trim(strings.Join(c.GetStrings("type"), ","), ","))
		shopold := models.Query(`select password from mt_shop where id=` + input.Get("id"))
		password := fmt.Sprint(shopold[0]["password"])
		if password != input.Get("password") {
			input.Set("password", sword.ToMD5(input.Get("password")))
		}
		res, err = models.Save("mt_shop", input)
	}
	if err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
		var r models.Mt_systems_record
		r.Tid = int(res)
		if input.Get("id") == "" {
			r.Way = 2
		} else {
			r.Way = 3
		}
		r.Addtime = sword.Timeget()
		r.Table = 26
		r.Uid, _ = strconv.Atoi(c.Qid)
		if _, err := models.Orm().Insert(&r); err != nil {
			beego.Debug(err)
		}
	}
}

type SystemPermission struct {
	BaseController
}

func (c *SystemPermission) Get() {
	page, page2, num := models.Pagechange(c.GetString("page"), "10")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.addtime desc`
	sql := `select a.* from mt_systems_permissions a where a.status=1 ` + ob + lt
	sql2 := `select count(a.id) as totals from mt_systems_permissions a where a.status=1`
	info := models.Query(sql)
	c.Data["info"] = info
	c.Data["paginator"] = models.Page(sql2, page2, num)
	c.Data["url"] = `/admin/driver/permission?`
	c.TplName = "admin/systems/permission.html"
}

type SystemAdmin struct {
	BaseController
}

func (c *SystemAdmin) Get() {
	page, page2, num := models.Pagechange(c.GetString("page"), "10")
	var w []string
	w = append(w, `a.status=1`)
	censoring := c.GetString("censoring")
	c.Data["censoring"] = censoring
	if censoring != "" {
		w = append(w, `a.censoring=`+censoring)
	}
	if c.Gid != "0" {
		w = append(w, `a.gid=`+c.Gid)
	}
	ws := strings.Join(w, " and ")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.addtime desc`
	sql := `select a.*,b.pathname,c.name,d.name as pname from mt_systems_admin a left join mt_resources b on a.avatar=b.id left join mt_shop c on a.gid=c.id 
	left join mt_systems_permissions d on a.permission=d.id where ` + ws + ob + lt
	sql2 := `select count(a.id) as totals from mt_systems_admin a where ` + ws
	info := models.Query(sql)
	c.Data["info"] = info
	c.Data["paginator"] = models.Page(sql2, page2, num)
	c.Data["url"] = `/admin/systems/admin?censoring=` + censoring + `&`
	c.TplName = "admin/systems/admin.html"
}

type SystemPermissionAE struct {
	BaseController
}

func (c *SystemPermissionAE) Get() {
	nodes := models.Query(`select * from mt_nav_admin where status=1 and pid=0`)
	for _, v := range nodes {
		v["son"] = models.Query(`select * from mt_nav_admin where pid=` + v["id"].(string))
	}
	c.Data["nodes"] = nodes
	id := c.GetString("id")
	if id != "" {
		if info := models.Query(`select a.* from mt_systems_permissions a where a.id=` + id); info != nil {
			c.Data["info"] = info[0]
		} else {
			c.Ctx.Redirect(302, "/admin/systems/permission")
		}
	}
	c.TplName = "admin/systems/permission_ae.html"
}
func (c *SystemPermissionAE) Post() {
	defer c.ServeJSON()
	input := c.Input()
	id := c.GetString("id")
	input.Set("nodes", strings.Trim(strings.Join(c.GetStrings("nodes"), ","), ","))
	if id == "" {
		input.Set("addtime", sword.Gettimes("2006-01-02 15:04:05"))
	}
	if res, err := models.Save("mt_systems_permissions", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
		var r models.Mt_systems_record
		r.Tid = int(res)
		if input.Get("id") == "" {
			r.Way = 2
		} else {
			r.Way = 3
		}
		r.Addtime = sword.Timeget()
		r.Table = 27
		r.Uid, _ = strconv.Atoi(c.Qid)
		if _, err := models.Orm().Insert(&r); err != nil {
			beego.Debug(err)
		}
	}
}

type SystemAdminAE struct {
	BaseController
}

func (c *SystemAdminAE) Get() {
	c.Data["permission"] = models.Query(`select * from mt_systems_permissions where status=1`)
	id := c.GetString("id")
	if id != "" {
		c.Data["info"] = models.Query(`select a.* from mt_systems_admin a where a.id=` + id)[0]
	}
	c.TplName = "admin/systems/ae.html"
}

func (c *SystemAdminAE) Post() {
	defer c.ServeJSON()
	input := c.Input()
	id := c.GetString("id")
	if id == "" {
		input.Set("addtime", sword.Gettimes("2006-01-02 15:04:05"))
		if is := models.Query(`select id from mt_systems_admin where accountnumber='` + input.Get("accountnumber") + `'`); is != nil {
			c.Data["json"] = 2
		} else {
			input.Set("password", sword.ToMD5(input.Get("password")))
			if res, err := models.Save("mt_systems_admin", input); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
			} else {
				c.Data["json"] = 1
				var r models.Mt_systems_record
				r.Tid = int(res)
				if input.Get("id") == "" {
					r.Way = 2
				} else {
					r.Way = 3
				}
				r.Addtime = sword.Timeget()
				r.Table = 28
				r.Uid, _ = strconv.Atoi(c.Qid)
				if _, err := models.Orm().Insert(&r); err != nil {
					beego.Debug(err)
				}
			}
		}
	} else {
		if is := models.Query(`select id from mt_systems_admin where id<>` + id + ` and accountnumber='` + input.Get("accountnumber") + `'`); is != nil {
			c.Data["json"] = 2
		} else {
			shopold := models.Query(`select password from mt_systems_admin where id=` + id)
			password := fmt.Sprint(shopold[0]["password"])
			if password != input.Get("password") {
				input.Set("password", sword.ToMD5(input.Get("password")))
			}
			if res, err := models.Save("mt_systems_admin", input); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
			} else {
				c.Data["json"] = 1
				var r models.Mt_systems_record
				r.Tid = int(res)
				if input.Get("id") == "" {
					r.Way = 2
				} else {
					r.Way = 3
				}
				r.Addtime = sword.Timeget()
				r.Table = 28
				r.Uid, _ = strconv.Atoi(c.Qid)
				if _, err := models.Orm().Insert(&r); err != nil {
					beego.Debug(err)
				}
			}
		}
	}

}

type SystemResources struct {
	BaseController
}

func (c *SystemResources) Get() {
	page, page2, num := models.Pagechange(c.GetString("page"), "30")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.addtime desc`
	sql := `select a.* from mt_resources a ` + ob + lt
	sql2 := `select count(a.id) as totals from mt_resources a `
	info := models.Query(sql)
	c.Data["info"] = info
	c.Data["paginator"] = models.Page(sql2, page2, num)
	c.Data["url"] = `/admin/systems/resources?`
	c.TplName = "admin/systems/resources.html"
}

type SystemArea struct {
	BaseController
}

func (c *SystemArea) Get() {
	page, page2, num := models.Pagechange(c.GetString("page"), "10")
	lt := ` limit ` + page + `,` + num
	province := models.Query(`select * from mt_area where pid=0` + lt)
	for _, v := range province {
		city := models.Query(`select * from mt_area where pid=` + v["id"].(string))
		for _, c := range city {
			c["area"] = models.Query(`select * from mt_area where pid=` + c["id"].(string))
		}
		v["city"] = city
	}
	c.Data["info"] = province
	sql2 := `select count(*) as totals from mt_area where pid=0`
	c.Data["paginator"] = models.Page(sql2, page2, num)
	c.Data["url"] = `/admin/systems/area?`
	c.TplName = "admin/systems/area.html"
}

type SystemLogin struct {
	BaseController
}

func (c *SystemLogin) Get() {
	page, page2, num := models.Pagechange(c.GetString("page"), "20")
	lt := ` limit ` + page + `,` + num
	sql := `select a.*,b.accountnumber,c.pathname from mt_systems_login a left join mt_systems_admin b on a.uid=b.id left join mt_resources c 
	on b.avatar=c.id order by a.logintime desc` + lt
	sql2 := `select count(*) as totals from mt_systems_login`
	c.Data["info"] = models.Query(sql)
	c.Data["paginator"] = models.Page(sql2, page2, num)
	c.Data["url"] = `/admin/systems/login?`
	c.TplName = "admin/systems/login.html"
}

type SystemRecord struct {
	BaseController
}

func (c *SystemRecord) Get() {
	page, page2, num := models.Pagechange(c.GetString("page"), "20")
	lt := ` limit ` + page + `,` + num
	var w []string
	w = append(w, `a.tid>0`)
	// w = append(w, `b.gid=`+c.Gid)
	id := c.GetString("id")
	if id != "" {
		w = append(w, `a.uid=`+id)
	}

	table := c.GetString("table")
	tid := c.GetString("tid")
	stime := c.GetString("stime")
	etime := c.GetString("etime")
	c.Data["tableid"] = table
	c.Data["tid"] = tid
	c.Data["stime"] = stime
	c.Data["etime"] = etime
	if table != "" {
		w = append(w, `a.table=`+table)
	}
	if tid != "" {
		w = append(w, `a.tid=`+tid)
	}
	if stime != "" {
		w = append(w, `a.addtime>'`+stime+` 00:00:00`+`'`)
	}
	if etime != "" {
		w = append(w, `a.addtime<='`+etime+` 00:00:00`+`'`)
	}

	ws := strings.Join(w, ` and `)
	sql := `
		select a.*,b.accountnumber,c.pathname,d.name as tname,e.name as wname 
		from mt_systems_record a 
		join (select * from mt_systems_admin where gid=` + c.Gid + `) b on a.uid=b.id 
		left join mt_resources c on b.avatar=c.id 
		left join mt_systems_configuration d on a.table=d.id 
		left join mt_systems_configuration e on a.way=e.id 
		where ` + ws + ` order by a.addtime desc` + lt
	info := models.Query(sql)
	for _, v := range info {
		table := fmt.Sprint(v["table"])
		tid := fmt.Sprint(v["tid"])
		way := fmt.Sprint(v["way"])
		switch table {
		case "9":
			if way == "77" {
				v["name"] = v["content"]
				v["url"] = "/admin/member/content?id=" + tid
			} else {

				name := models.Query(`select monicker from mt_member where id=` + tid)
				if name != nil {
					v["name"] = name[0]["monicker"]
					v["url"] = "/admin/member/content?id=" + tid
				}
			}
		case "10":
			name := models.Query(`select name from mt_member_vip where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/member/vip_edit?id=" + tid
			}
		case "11":
			name := models.Query(`select name from mt_ad where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/index/ad_edit?id=" + tid
			}
		case "12":
			name := models.Query(`select title from mt_keywords where id=` + tid)
			if name != nil {
				v["name"] = name[0]["title"]

			}
		case "13":
			name := models.Query(`select name from mt_shop where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/shop/content?id=" + tid
			}
		case "14":
			name := models.Query(`select name,type from mt_column where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				if fmt.Sprint(name[0]["type"]) == "p" {
					v["url"] = "/admin/product/column_content?id=" + tid
				}
			}
		case "16":
			name := models.Query(`select title from mt_product where id=` + tid)
			if name != nil {
				v["name"] = name[0]["title"]
				v["url"] = "/admin/product/content?id=" + tid
			}
		case "18":
			//订单
			name := models.Query(`select ordernum from mt_order where id=` + tid)
			if name != nil {
				v["name"] = name[0]["ordernum"]
				v["url"] = "/admin/order/content?id=" + tid
			}
		case "21":
			name := models.Query(`select title from mt_news where id=` + tid)
			if name != nil {
				v["name"] = name[0]["title"]
				v["url"] = "/admin/news/content?id=" + tid
			}
		case "23":
			name := models.Query(`select name from mt_page where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/page/content?id=" + tid
			}
		case "26":
			v["name"] = "系统设置"
			v["url"] = "/admin/system/index"
			//系统设置
		case "27":
			name := models.Query(`select name from mt_systems_permissions where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/systems/permission_edit?id=" + tid
			}
		case "28":
			name := models.Query(`select accountnumber from mt_systems_admin where id=` + tid)
			if name != nil {
				v["name"] = name[0]["accountnumber"]
				v["url"] = "/admin/systems/admin_edit?id=" + tid
			}
		case "29":
			name := models.Query(`select name from mt_area where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/systems/area_edit?id=" + tid
			}
		case "34":
			name := models.Query(`select name from mt_brand where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/brand/content?id=" + tid
			}
		case "35":
			name := models.Query(`select pid,title from mt_product_spec where id=` + tid)
			if name != nil {
				v["name"] = name[0]["title"]
				v["url"] = "/admin/product/content?id=" + name[0]["pid"].(string)
			}
		case "36":
			name := models.Query(`select a.key,a.pid from mt_product_parameter a where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["key"]
				v["url"] = "/admin/product/content?id=" + name[0]["pid"].(string)
			}
		case "37":
			name := models.Query(`select cid,name from mt_product_attribute_column where id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/product/column_content?id=" + name[0]["cid"].(string)
			}
		case "38":
			name := models.Query(`select b.cid,a.name from mt_product_attribute a left join mt_product_attribute_column b on a.cid=b.id where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/product/column_content?id=" + name[0]["cid"].(string)
			}
		case "39":
			name := models.Query(`select a.name from mt_coupon a where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/coupon/content?id=" + tid
			}
		case "58":
			name := models.Query(`select b.monicker from mt_promote_add a left join mt_member b on a.uid=b.id where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["monicker"]
				v["url"] = "/admin/member/promote_apply"
			}
		case "64":
			name := models.Query(`select a.title from mt_active_gbuy a  where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["title"]
				v["url"] = "/admin/active/gbuy?id=" + tid
			}

		case "71":
			name := models.Query(`select b.monicker from mt_member_getcash a left join mt_member b on a.uid=b.id where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["monicker"]
				v["url"] = "/admin/member/getcash"
			}
		case "73":
			name := models.Query(`select b.title,a.pid from mt_member_integral a left join mt_product_integral b on a.pid=b.id where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["monicker"]
				v["url"] = "/admin/product/integral_content?id=" + name[0]["pid"].(string)
			}

		case "74":
			name := models.Query(`select b.name from mt_feedback a left join mt_shop b on a.gid=b.id where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/index/feedback?"
			}

		case "75":
			name := models.Query(`select a.title from mt_active_gbuy a  where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["title"]
				v["url"] = "/admin/active/sales?id=" + tid
			}
		case "76":
			name := models.Query(`select a.name from mt_program a where a.id=` + tid)
			if name != nil {
				v["name"] = name[0]["name"]
				v["url"] = "/admin/program/edit?id=" + tid
			}
		case "80":
			name := models.Query(`select a.monicker from mt_member a 
				left join mt_business b on a.id=b.uid 
				where b.id=?`, tid)
			if name != nil {
				v["name"] = name[0]["monicker"]
				v["url"] = "/admin/member/business_content?id=" + tid
			}
		}
	}
	sql2 := `select count(a.id) as totals from mt_systems_record a join (select * from mt_systems_admin where gid=` + c.Gid + `) b on a.uid=b.id where ` + ws
	c.Data["info"] = info
	c.Data["paginator"] = models.Page(sql2, page2, num)
	if id != "" {
		c.Data["url"] = `/admin/systems/admin_record?id=` + id + `&`
	} else {
		c.Data["url"] = `/admin/systems/record?`
	}
	c.TplName = "admin/systems/record.html"
}

type SystemAreaAE struct {
	BaseController
}

func (c *SystemAreaAE) Get() {
	province := models.Query(`select id,name from mt_area where pid=0 and status=1`)
	for _, v := range province {
		v["city"] = models.Query(`select id,name from mt_area where pid=` + v["id"].(string) + ` and status=1`)
	}
	c.Data["province"] = province
	id := c.GetString("id")
	if id != "" {
		if info := models.Query(`select * from mt_area where id=` + id); info != nil {
			c.Data["info"] = info[0]
		} else {
			c.Ctx.Redirect(302, "/admin/systems/area")
		}
	}
	c.TplName = "admin/systems/area_ae.html"
}
func (c *SystemAreaAE) Post() {
	defer c.ServeJSON()
	input := c.Input()
	pid := input.Get("pid")
	if pid == "0" {
		input.Set("level", "1")
	} else {
		level := models.Query(`select level from mt_area where id=` + pid)[0]["level"].(string)
		leveli, _ := strconv.Atoi(level)
		leveli += 1
		input.Set("level", fmt.Sprint(leveli))
	}
	if res, err := models.Save("mt_area", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
		var r models.Mt_systems_record
		r.Tid = int(res)
		if input.Get("id") == "" {
			r.Way = 2
		} else {
			r.Way = 3
		}
		r.Addtime = sword.Timeget()
		r.Table = 29
		r.Uid, _ = strconv.Atoi(c.Qid)
		if _, err := models.Orm().Insert(&r); err != nil {
			beego.Debug(err)
		}
	}
}

type SystemService struct {
	BaseController
}

func (c *SystemService) Get() {
	sql := `select a.*,c.pathname from mt_member a left join mt_resources c 
	on a.avatar=c.id where service=1 and gid=` + c.Gid + ` order by a.addtime desc`
	info := models.Query(sql)
	for _, v := range info {
		if servicearea := fmt.Sprint(v["servicearea"]); servicearea != "" && servicearea != "<nil>" {
			servicearea = strings.Trim(servicearea, ",")
			v["dq"] = models.Query(`select * from mt_area where id in(` + servicearea + `)`)
		}
	}
	c.Data["info"] = info
	c.TplName = "admin/systems/service.html"
}

type SystemServiceAE struct {
	BaseController
}

func (c *SystemServiceAE) Get() {
	id := c.GetString("id")
	c.Data["area"] = models.Query(`select id,name from mt_area where pid=0`)
	if id != "" {
		if info := models.Query(`select * from mt_member where service=1 and id=` + id); info != nil {
			c.Data["info"] = info[0]
		} else {
			c.Ctx.Redirect(302, "/admin/systems/servic")
		}
	}
	c.TplName = "admin/systems/service_ae.html"
}
func (c *SystemServiceAE) Post() {
	id := c.GetString("id")
	servicearea := "," + strings.Join(c.GetStrings("servicearea"), ",") + ","
	if err := models.Query(`update mt_member set service=1,servicearea='` + servicearea + `',gid=` + c.Gid + ` where id=` + id); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type SystemServiceDel struct {
	BaseController
}

func (c *SystemServiceDel) Get() {
	id := c.GetString("id")
	if err := models.Query(`update mt_member set service=0,gid=0 where id=` + id); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		models.Exec(`update mt_member set sid=0 where sid=?`, id)
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type SystemDriver struct {
	BaseController
}

func (c *SystemDriver) Get() {
	sql := `select a.*,c.pathname,d.name as dq from mt_member a left join mt_resources c 
	on a.avatar=c.id left join mt_area d on a.driverarea=d.id where a.driver=1 order by a.addtime desc`
	info := models.Query(sql)
	c.Data["info"] = info
	c.TplName = "admin/systems/driver.html"
}

type SystemDriverAE struct {
	BaseController
}

func (c *SystemDriverAE) Get() {
	id := c.GetString("id")
	c.Data["area"] = models.Query(`select id,code,name from mt_area where pid=1142`)
	if id != "" {
		if info := models.Query(`select * from mt_member where driver=1 and id=` + id); info != nil {
			c.Data["info"] = info[0]
		} else {
			c.Ctx.Redirect(302, "/admin/systems/servic")
		}
	}
	c.TplName = "admin/systems/driver_ae.html"
}
func (c *SystemDriverAE) Post() {
	id := c.GetString("id")
	driverarea := "," + strings.Join(c.GetStrings("driverarea"), ",") + ","
	if err := models.Query(`update mt_member set driver=1,driverarea='` + driverarea + `' where id=` + id); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type SystemDriverDel struct {
	BaseController
}

func (c *SystemDriverDel) Get() {
	id := c.GetString("id")
	if err := models.Query(`update mt_member set driver=0,gid=0 where id=` + id); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type SystemServiceList struct {
	beego.Controller
}

func (c *SystemServiceList) Get() {
	// c.SetSession("pc", "7")

	uid := c.GetString("id")

	sql := `select * from mt_sms where id in (select max(a.id) from mt_sms a where (fid=` + uid + ` or uid=` + uid + `) and uid<>0 and fid<>0 and status=1 group by (a.fid+a.uid)) order by addtime desc`
	info := models.Query(sql)
	var id string
	lt := ` limit 0,20`
	var index = 0
	for _, v := range info {
		fid := v["fid"].(string)
		sid := v["uid"].(string)
		if uid == fid {
			v["member"] = models.Query(`select a.monicker,b.pathname,a.id,a.country,a.province,a.city from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + sid)[0]
			id = sid
		} else {
			v["member"] = models.Query(`select a.monicker,b.pathname,a.id,a.country,a.province,a.city from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + fid)[0]
			id = fid
		}
		fidi, _ := strconv.Atoi(fid)
		sidi, _ := strconv.Atoi(sid)
		idsum := fidi + sidi
		v["noread"] = models.Query(fmt.Sprintf(`select count(*) as num from mt_sms where uid+fid=%v and uid=%v and state=0 and status=1`, idsum, uid))[0]["num"]
		sql2 := `select a.* from (select * from mt_sms where (uid=` + id + ` and fid=` + uid + `) or
		 (uid=` + uid + ` and fid=` + id + `) order by id desc ` + lt + `) a order by a.id`
		info2 := models.Query(sql2)
		v["first"] = models.Query(`select a.monicker,a.id,b.pathname from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + uid)[0]
		v["second"] = models.Query(`select a.monicker,a.id,b.pathname from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + id)[0]
		v["info"] = info2
	}
	c.Data["json"] = info
	c.Data["uid"] = uid
	c.Data["index"] = index
	isservice := 0
	iss := models.Query(`select id from mt_member where id=? and service=1`, uid)
	if iss != nil {
		isservice = 1
	}
	isshop := models.Query(`select id from mt_shop where status=1 and censoring=1 and uid=?`, uid)
	if isshop != nil {
		isservice = 1
	}
	c.Data["isservice"] = isservice
	c.TplName = "admin/service/index.html"
}
